﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///FormatTime 的摘要说明
/// </summary>
public class FormatTime
{
    public static string format(string time)
    {
        string return_time="";//返回时间
        DateTime date = Convert.ToDateTime(time);
        DateTime now = DateTime.Now;
        TimeSpan date_ts = new TimeSpan(date.Ticks);//需要格式的时间
        TimeSpan now_ts = new TimeSpan(now.Ticks);//现在的时间
        TimeSpan ts = now_ts.Subtract(date_ts);//时间差
        double time_space = ts.TotalSeconds;
        if(time_space<60)
		{
            return return_time = "刚刚";
		}
		else if(time_space<60*60)
		{
            return return_time = Convert.ToInt32(time_space / 60) + "分钟前";
		}
		else if(time_space<24*60*60&&date.Day==now.Day)
		{
            return return_time = "今天" + date.Hour + ":" + date.Minute;
		}
        else if (time_space < 2 * 24 * 60 * 60  && date.Day == now.Day - 1)
		{
            return return_time = "昨天" + date.Hour + ":" + date.Minute;
		}
        else if (time_space < 3 * 24 * 60 * 60  && date.Day == now.Day - 2)
		{
            return return_time = "前天" + date.Hour + ":" + date.Minute;
		}
		else if(date.Year==now.Year)
		{
            return return_time = date.Month + "月" + date.Day + "日 " + date.Hour + ":" + date.Minute;
		}
		else
		{
            return return_time = date.Year + "年" + date.Month + "月" + date.Day + "日 ";
		}
    }
}